//Eli Elder
//testInitialization

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "interface.h"

#define FORCEUNIQUE 0
#define PRINTOUT 1
#define ASSERTIVE 1

int main(){
    //define variables
    int i;
    int j;
    int randomSeed;
    int kingCard;
    int kingCards[10];
    int cardFound;
    int players;
    int returnVal;
    int coppers;
    int coppersInHand;
    char cardName[30];
    int coppersTotal;
    struct gameState theGameState; 
    
    //setup the gamestate as blank
    memset(&theGameState, 0, sizeof(struct gameState));
    
    //get the random number of players between 1 and 5
    players = floor(rand() % 5) + 1;
    
    i = 0;
    //get the 10 random kingdom cards
    while(i < 10){
        //start off saying we don't have it
        cardFound = 0;
        //kingdom cards are from 7 to 26 else I would use the car names
        kingCard = floor(rand() % 20) + 7;
        
        //loop through to see if we have it already
        for(j = 0; j <= i; j++){
            //if we have the card say so
            if(kingCard == kingCards[j]){
                cardFound = 1;
            }
        }
	
#if (FORCEUNIQUE == 1)
        kingCards[i] = kingCard;
        i++
#endif
	    //have a force unique because we will get non unique a lot I bet
#if (FORCEUNIQUE == 0)
	    //if we havent found the card set it for our game
	    if(cardFound == 0){
		kingCards[i] = kingCard;
		cardFound = 0;
		//also go to next spot
		i++;
	    }
	
#endif
    }
    
    //this is where we call the initialize
    randomSeed = 2;
    returnVal = initializeGame(players, kingCards, randomSeed, &theGameState);
    
#if (PRINTOUT == 1)
    if(players > 4 || players < 2){
        printf("Should only have between 2 and 4 players, you have %d\n", players);
    }else{
        printf("Amount of players is correct at %d\n", players);
    }
    if(cardFound == 1){
        printf("There are non-unique kingdom cards\n");
    }else{
        printf("Only unique kingdomcards\n");
    }
#endif
#if (ASSERTIVE == 1)
    if(players > 4 || players < 2){
        //make sure it returned -1
        assert(returnVal == -1);
    }else{
        //make sure it returned 0
        assert(returnVal == 0);
    }
    if(cardFound == 1){
        //make sure it returns -1
        assert(returnVal == -1);
    }else{
        //make sure it returns 0 (hopefully)
        assert(returnVal == 0);
    }
#endif
#if (PRINTOUT == 1)
    //check the number of curse cards
    if(players == 2){
        //should have 10
        if(theGameState.supplyCount[curse] == 10){
            printf("The supply count for curse is 10, good\n");
        }else{
            printf("The supply count for curse is %d, it should be 10\n", theGameState.supplyCount[curse]);
        }
    }else if (players == 3){
        //should hvae 20
        if(theGameState.supplyCount[curse] == 20){
            printf("The supply count for curse is 20, good\n");
        }else{
            printf("The supply count for curse is %d, it should be 20\n", theGameState.supplyCount[curse]);
        }
    }else if(players == 4){
        //should have 30
        if(theGameState.supplyCount[curse] == 30){
            printf("The supply count for curse is 30, good\n");
        }else{
            printf("The supply count for curse is %d, it should be 30\n", theGameState.supplyCount[curse]);
        }
    }
    
    //chcek the number of victory cards
    if(players == 2){
        //should have 8 of each
        if(theGameState.supplyCount[estate] == 8){
            printf("The supply count for estate is 8, good\n");
        }else{
            printf("The supply count for estate is %d, should be 8\n", theGameState.supplyCount[estate]);
        }
	if(theGameState.supplyCount[duchy] == 8){
            printf("The supply count for duchy is 8, good\n");
        }else{
            printf("The supply count for duchy is %d, should be 8\n", theGameState.supplyCount[duchy]);
        }
	if(theGameState.supplyCount[province] == 8){
            printf("The supply count for province is 8, good\n");
        }else{
            printf("The supply count for province is %d, should be 8\n", theGameState.supplyCount[province]);
        }
    }else{
        //should be 12 of ecah victory
        if(theGameState.supplyCount[estate] == 12){
            printf("The supply count for estate is 12, good\n");
        }else{
            printf("The supply count for estate is %d, should be 12\n", theGameState.supplyCount[estate]);
        }
	if(theGameState.supplyCount[duchy] == 12){
            printf("The supply count for duchy is 12, good\n");
        }else{
            printf("The supply count for duchy is %d, should be 12\n", theGameState.supplyCount[duchy]);
        }
	if(theGameState.supplyCount[province] == 12){
            printf("The supply count for province is 12, good\n");
        }else{
            printf("The supply count for province is %d, should be 12\n", theGameState.supplyCount[province]);
        }
    }
    
    //check the treasure cards available
    for(i = 0; i < players; i++){
        coppers += fullDeckCount(i, copper, &theGameState);
    }
    
    coppersTotal = coppers + supplyCount(copper, &theGameState);
    //check there are 60 total coppers
    if(coppersTotal == 60){
	printf("The total supply count for copper is 60, good\n");
    }else{
	printf("The supply count for copper is %d, it should be 60\n", (coppersTotal));
    }
    
    
    //check that the supply has 60 - 7 * players of coppers
    if(theGameState.supplyCount[copper] == 60 - 7 * players){
        printf("The supply count of copper is %d, that is good, should be 46 for 2 players, 39 for 3 players, and 32 for 4 players\n", theGameState.supplyCount[copper]);
    }else{
        printf("The supply count of copper is %d, not good, should be 46 for 2 players, 39 for 3 players, and 32 for 4 players\n", theGameState.supplyCount[copper]);
    }
    
    //check 40 silvers
    if(theGameState.supplyCount[silver] == 40){
        printf("The supply count for gold is 40, good\n");
    }else{
        printf("The supply count for silver is %d, it should be 40\n", theGameState.supplyCount[silver]);
    }
    
    //check 30 gold
    if(theGameState.supplyCount[gold] == 30){
        printf("The supply count for gold is 30, good\n");
    }else{
        printf("The supply count for gold is %d, it should be 30\n", theGameState.supplyCount[gold]);
    }
    
	int supplyTen = 0;
    //check if all kingdom cards exist in our ten and check supply
    for(i = 0; i <= treasure_map; i++){
        for(j = 0; j < 10; j++){
			if(kingCards[j] == i){
				//flag that it is a supply we want
				supplyTen = 1;
			}
		}
			//we have supply do the if else do the else
            if(supplyTen == 1){
                if(i == great_hall || i == gardens || i == gold || i == silver || i == copper){
                    if(players == 2){
                        //check supply is 8
                        if(theGameState.supplyCount[i] == 8){
			    cardNumToName(i, cardName);
			    printf("The supply count for card %s is 8, good\n", cardName);
			}else{
			    cardNumToName(i, cardName);
			    printf("The supply count for card %s is %d, should be 8\n", cardName,theGameState.supplyCount[i]);
			}
                    }else{
                        //check supply is 12
                        if(theGameState.supplyCount[i] == 12){
			    cardNumToName(i, cardName);
			    printf("The supply count for card %s is 12, good\n", cardName);
			}else{
			    cardNumToName(i, cardName);
			    printf("The supply count for card %s is %d, should be 12\n", cardName,theGameState.supplyCount[i]);
			}
                    }
                }else{
                    //check supply is 10
                    if(theGameState.supplyCount[i] == 10){
                        cardNumToName(i, cardName);
			printf("The supply count for card %s is 10, good\n", cardName);
                    }else{
                        cardNumToName(i, cardName);
			printf("The supply count for card %s is %d, should be 10\n", cardName,theGameState.supplyCount[i]);
                    }
                }
            }else{
		//check supply is -1 or whatever
		if(theGameState.supplyCount[i] == -1){
		    cardNumToName(i, cardName);
		    printf("The supply for card %s is -1, good\n", cardName);
		}else{
		    cardNumToName(i, cardName);
		    printf("The supply for card %s is %d, should be -1\n", cardName, theGameState.supplyCount[i]);
		}
            }
        
        
        //check embargo tokens are all 0
        if(theGameState.embargoTokens[i] == 0){
	    cardNumToName(i, cardName);
            printf("There are 0 embargo tokens for card %s\n", cardName);
        }else{
	    cardNumToName(i, cardName);
            printf("There are %d embargo tokens for card %s\n", i, cardName);
        }
		//reset that the supply exists
		supplyTen = 0;
    }
    
    //check player card stuff
    for(i = 0; i < players; i++){
        //check 3 estates
        if(fullDeckCount(i, estate, &theGameState) == 3){
            printf("Player %d has 3 estates, good\n", i);
        }else{
            printf("Player %d has %d estates, should have 3\n", i, fullDeckCount(i, estate, &theGameState));
        }
        
        //check 7coppers
        if(fullDeckCount(i, copper, &theGameState) == 7){
            printf("Player %d has 7 coppers, good\n", i);
        }else{
            printf("Player %d has %d coppers, should have 7\n", i, fullDeckCount(i, copper, &theGameState));
        }
        
        //check each have 10 (well if 3 and 7 work then 10 will, but whatever
        if(theGameState.handCount[i] + theGameState.deckCount[i] == 10){
            printf("Player %d has 10 total cards, good\n", i);
        }else{
            printf("Player %d has %d total cards, should have 10\n", i, theGameState.handCount[i] + theGameState.deckCount[i]);
        }
        
        //check the handcount for non first players is 0
        if(i != theGameState.whoseTurn){
            if(theGameState.handCount[i] == 0){
                printf("The handcount of the non-first player is 0, good\n");
            }else{
                printf("The handcount of the non-first player is %d, bad\n", theGameState.handCount[i]);
            }
        }
    }
    //these are gamestate so not for each player, just the first player
    //check outpost, phase, played card, whoseturn 0
    if(theGameState.outpostPlayed == 0){
        printf("The outpost played flag is 0, good\n");
    }else{
        printf("The outpost flag is set to %d, bad\n", theGameState.outpostPlayed);
    }
    if(theGameState.phase == 0){
        printf("The phase is set to 0\n");
    }else{
        printf("The phase is set to %d, bad\n",theGameState.phase);
    }
    if(theGameState.whoseTurn == 0){
        printf("The persons whose turn it is is 0, good\n");
    }else{
        printf("The persons whose turn it is is %d, bad\n",theGameState.whoseTurn);
    }
    
    
    
    //check num actions and buys are 1
    if(theGameState.numActions == 1){
        printf("The number of actions is 1, yay\n");
    }else{
        printf("The number of actions is not 1, it is %d which is bad\n", theGameState.numActions);
    }
    if(theGameState.numBuys == 1){
        printf("The number of buys is 1, yay\n");
    }else{
        printf("The number of buys is not 1, it is %d, which is bad\n", theGameState.numBuys);
    }
    
    //check that first player has 5 cards in his or her hand
    if(theGameState.handCount[whoseTurn(&theGameState)] == 5){
        printf("The number of cards in the first players hand is 5\n");
    }else{
        printf("The number of cards inthe first players hand is not 5, it is %d\n", theGameState.handCount[whoseTurn(&theGameState)]);
    }
    
    //check if update coins for this person is correct, coins == copper in hand
    coppersInHand = 0;
    for(i = 0; i < theGameState.handCount[whoseTurn(&theGameState)]; i++){
		if(theGameState.handCount[i] == copper){
        coppersInHand++;
		}
    }
    if(theGameState.coins == coppersInHand){
        printf("number of coins is equal to coppers in hand at %d and %d\n", theGameState.coins, coppersInHand);
    }else{
        printf("number of coins and copper in hand is incorrect at %d and %d\n", theGameState.coins, coppersInHand);
    }
#endif
    return 0;
}
